package fr.univ.surveyonline.mailling;

import java.io.UnsupportedEncodingException;
import java.util.Properties; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.AddressException; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class Mail {
	 
	private static String admin = "caillotyohann@gmail.com";
	private Properties props = new Properties(); 
	private Session session = Session.getDefaultInstance(props, null); 	
	
	public Mail() {
		// TODO Auto-generated constructor stub
	}
	
	 public void send(String address, String subject, String text) {
		 try { 
			 Properties props = new Properties(); 
			 Session session = Session.getDefaultInstance(props, null); 
			 Message msg = new MimeMessage(session); 
			 // Provenance du message
			 msg.setFrom(new InternetAddress(admin)); 
			 // Adresse TO
			 msg.addRecipient(Message.RecipientType.TO, 
					 new InternetAddress(address, "Destinataire")); 
			// Adresse CC
		    //msg.addRecipient(Message.RecipientType.CC, 
		    //		 new InternetAddress(admin, "Administrateur"));
		     
			 msg.setSubject(subject); 
			 // Message
			 msg.setText(text); 
			 // Envoi
			 Transport.send(msg); 
			 
		 } catch (AddressException e) { 
			 e.printStackTrace(); 
		 } catch (MessagingException e) { 
			 e.printStackTrace();
		 } catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}	
		 
	 }
}
